<?php

namespace App\Kits\Resources;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

/**
 * JsonResponse类型转换（自定义格式化输出）
 * Class ApiJsonResponse
 * @package App\Kits\Resources
 */
class ApiJsonResponse extends JsonResponse
{
    // 不转义中文和斜杠
    protected $encodingOptions = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;

    /**
     * @param Request $request
     * @param JsonResponse $response
     * @return ApiJsonResponse
     */
    static public function jsonConvertToCustomJsonResponse(Request $request, JsonResponse $response): self
    {
        $resource = new ApiResourceResponse($response->getData());
        $response = $resource->toResponse($request);
        return new self($response->getData(), $response->getStatusCode(), $response->headers->all(), $response->getEncodingOptions());
    }

    static public function NullConvertToCustomJsonResponse(Request $request, Response $response): self
    {
        $resource = new ApiResourceResponse(null);
        $response = $resource->toResponse($request);
        return new self($response->getData(), $response->getStatusCode(), $response->headers->all(), $response->getEncodingOptions());
    }
}
